Imports System
Imports System.Configuration
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms

Public Class Config

#Region "Properties"

	Private Shared _Encoding As Text.Encoding = Text.Encoding.UTF8
	Private Shared _MaxRetry As Integer = 2
	Private Shared _RetryInterval As Integer = 500
	Private Shared _DefaultConnectionString As String
	Private Shared _DefaultProvider As ProviderEnum
	Private Shared _LogPath As String = "DatabaseLogs"
	Private Shared _LogEnabled As Boolean
	Private Shared _QueryFieldNullable As Boolean = True

	Public Shared Property Encoding() As Text.Encoding
		Get
			Return _Encoding
		End Get
		Set(ByVal value As Text.Encoding)
			_Encoding = value
		End Set
	End Property

	Public Shared Property MaxRetry() As Integer
		Get
			Return _MaxRetry
		End Get
		Set(ByVal value As Integer)
			_MaxRetry = value
		End Set
	End Property

	Public Shared Property RetryInterval() As Integer
		Get
			Return _RetryInterval
		End Get
		Set(ByVal value As Integer)
			_RetryInterval = value
		End Set
	End Property

	Public Shared Property DefaultConnectionString() As String
		Get
			Return _DefaultConnectionString
		End Get
		Set(ByVal value As String)
			_DefaultConnectionString = value
		End Set
	End Property

	Public Shared Property DefaultProvider() As ProviderEnum
		Get
			Return _DefaultProvider
		End Get
		Set(ByVal value As ProviderEnum)
			_DefaultProvider = value
		End Set
	End Property

	Public Shared Property LogPath() As String
		Get
			Return _LogPath
		End Get
		Set(ByVal value As String)

			Dim strBasePath As String

			If Utils.AssemblyUtil.GetAppType = Utils.AppTypeEnum.AspNet Then
				strBasePath = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) & "\"
			Else
				strBasePath = Path.GetDirectoryName(Application.ExecutablePath) & "\"
			End If

			If value.Length > 0 Then

				If Not Utils.IOUtil.ValidatePhysicalPath(value) Then
					value = strBasePath & value
				End If

				Try

					If value.Substring(value.Length - 1, 1) <> "\" AndAlso value.Substring(value.Length - 1, 1) <> "/" Then
						value += "\"
					End If

					If Utils.IOUtil.ValidatePhysicalPath(value) Then
						_LogPath = value
					Else
						Throw New UniDataTierException(GetType(Config), My.Resources.Messages.ErrorInvalidPath)
					End If

				Catch ex As Exception
					Throw New UniDataTierException(GetType(Config), My.Resources.Messages.ErrorInvalidPath)
				End Try

			End If

		End Set
	End Property

	Public Shared Property LogEnabled() As Boolean
		Get
			Return _LogEnabled
		End Get
		Set(ByVal value As Boolean)
			_LogEnabled = value
		End Set
	End Property

	Public Shared Property QueryFieldNullable() As Boolean
		Get
			Return _QueryFieldNullable
		End Get
		Set(ByVal Value As Boolean)
			_QueryFieldNullable = Value
		End Set
	End Property

#End Region

#Region "Load configuration"

	Public Shared Sub LoadFromXml()

		LoadFromXml("UniDataTier")

	End Sub

	Public Shared Sub LoadFromXml(ByVal strConnectionString As String)

		Dim objConnectionString As ConnectionStringSettings = Utils.ConfigUtil.GetConnectionString(strConnectionString)
		Dim intEncoding As Integer

		intEncoding = Utils.ConfigUtil.GetConfig("Devv.Encoding", 0)

		If intEncoding < 1 Then
			intEncoding = Utils.ConfigUtil.GetConfig("Devv.Core.UniDataTier.Encoding", _Encoding.CodePage)
		End If

		Encoding = Text.Encoding.GetEncoding(intEncoding)

		MaxRetry = Utils.ConfigUtil.GetConfig("Devv.Core.UniDataTier.MaxRetry", _MaxRetry)
		RetryInterval = Utils.ConfigUtil.GetConfig("Devv.Core.UniDataTier.RetryInterval", _RetryInterval)
		LogPath = Utils.ConfigUtil.GetConfig("Devv.Core.UniDataTier.LogPath", _LogPath)
		LogEnabled = Utils.ConfigUtil.GetConfig("Devv.Core.UniDataTier.DoLog", _LogEnabled)
		QueryFieldNullable = Utils.ConfigUtil.GetConfig("Devv.Core.UniDataTier.QueryFieldNullable", _QueryFieldNullable)
		DefaultConnectionString = objConnectionString.ConnectionString
		DefaultProvider = GetProvider(objConnectionString.ProviderName)

	End Sub

#End Region

#Region "Helper methods"

	Friend Shared Function GetProvider(ByVal strProvider As String) As ProviderEnum

		If strProvider Is Nothing OrElse strProvider = String.Empty Then
			Return Nothing
		End If

		If strProvider.Contains(".") Then
			strProvider = strProvider.Replace(".", "").ToLower()
		End If

		If strProvider.Contains("oracle") Then
			Return ProviderEnum.Oracle
		ElseIf strProvider.Contains("sqlclient") Then
			Return ProviderEnum.SqlServer
		ElseIf strProvider.Contains("odbc") Or strProvider.Contains("dsn") Then
			Return ProviderEnum.Odbc
		Else
			Return ProviderEnum.OleDb
		End If

	End Function

#End Region

End Class